package work;

public class test1 {
	public static boolean flag=false;
	public static int count=0;
	public static int num=10;
	public static final Object lock=new Object();
	public static void main(String[] args) {
		Foodie f=new Foodie();
		Cooker c=new Cooker();
		f.start();
		c.start();
	}
	public static class Cooker extends Thread {
		@Override
		public void run() {
			while(true){
				synchronized (lock) {
					if(num==0){
						break;
					}
					if(!flag){
						//第一次
						while(count<4){
							System.out.println("厨师正在生产汉堡包");
							count++;
						}
						System.out.println("厨师正在生产汉堡包");
						count++;
						flag=true;
						lock.notify();
					}else{
						//有
						try {
							lock.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		}
	}
	public static class Foodie extends Thread {
		@Override
		public void run() {
			while(true){
			 synchronized (lock) {
				if(num==0){
					break;
				}else{
					if(flag){
						//有
						System.out.println("吃货正在吃汉堡包");
						flag=false;
						lock.notify();
						num--;
						count--;
					}else{
						//没有
						try {
							lock.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
			}
		}
	}
	
}
